package com.jason.kafka;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.admin.NewTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

/**
 * 生产者
 */
@Service
@Slf4j
public class ProducerService {

    @Autowired
    KafkaTemplate template;

    @Autowired
    @Qualifier("initTopic")
    NewTopic newTopic;

    public void send2(String topic,String message){
        String msg = "hello kafka!  发送的数据:"+message;
        for (int i = 0; i < 10; i++) {
            template.send(topic, msg+"--->第"+i+"条数据");
        }
        log.info("---生产者发送题：{},发送了10条数据，消息内容:{}",topic,msg);
    }


    public void send(String topic,String message){
        String msg = "hello kafka!  发送的数据:"+message;
        for (int i = 0; i < 10; i++) {
            template.send(topic, msg+"--->第"+i+"条数据");
            template.send(newTopic.name(),i%3, null,msg+"--->第"+i+"条数据");

        }
        log.info("---生产者发送题：{},发送了10条数据，消息内容:{}",newTopic.name(),msg);
    }
}
